
import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";

// //引入全局配置变量
// import global from './config/global'

// Vue.prototype.global = global
// //挂载公共方法
// import base from './config/mothed' //引用
// Vue.use(base) //将全局函数当做插件来进行注册

// Vue.config.productionTip = false;

// 引入Element
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)

// 引入axios
import axios from "axios";
axios.defaults.withCredentials = true;
Vue.prototype.$http = axios;

// 引入公共方法
import common from './static/common'
Vue.prototype.common = common

// 引入iconfont
import iconfont from "./assets/iconfont/iconfont.css";
Vue.use(iconfont);

//引入md5加密


//引入less
//引入less
import './static/less/normalize.css'
import './static/less/base.less'

// 引入echarts
import echarts from 'echarts'

Vue.prototype.$echarts = echarts


// import VueResource from "vue-resource";
// Vue.use(VueResource);

//引入qs，解决修改删除向后台传数据问题
import qs from 'querystring'
// Vue.use(qs);

Vue.prototype.$axios = function(method, url, values, callback) {
  let $this = this;
  if (method == "post") {
    axios
      .post(url, qs.stringify(values), {
        headers: { "Content-Type": "application/x-www-form-urlencoded" },
        withCredentials: true
      })
      .then(function(response) {
        console.log(response);
        if (response.data.code == 0) {
          $this.message(true, response.data.message, "success");
          callback(response);
        } else if (response.data.code == 1) {
          $this.$message.error(response.data.message);
          console.log(response);
        } else if (response.data.code == 2) {
          // $this.$router.push(response.data.data);
          $this.message(true, response.data.message, "error");
          self.$router.push("/v/login/");
          // $this.$message.warn(response.data.message);
        } else if (response.data.code == 3) {
          $this.$message.warn(response.data.message);
        } else if (response.data.code == 4) {
          $this.message(true, response.data.message, "error");
          callback(response);
        } else {
          callback(response);
        }
      })
      .catch(err => {
        console.log(err);
        //   if(err.response.status == 422){
        //       $this.$message.error(err.response.data.errors.email)
        //   }
      });
  }
  // else {
  //     throw 'Illegal Method is Given'
  // }
};

Vue.prototype.message = function(judge, msg, type) {
  let _this = this;
  return _this.$message({
    showClose: judge,
    message: msg,
    type: type
  });
};

router.beforeEach((to, from, next) => {
  let _this = this;
  var t = to.path.split("/")[2];
  var account = common.getCookie("userName");
  if (to.path == "/collegeLogin" || to.path == "/login") return next();

  console.log(account != null);
  if (account != null) {
    return next();
  }

  if (t == "college") {
    alert("请登录之后再进行操作");
    return next({ path: "/collegeLogin" });
  } else if (t == "root" || t == "department" || t == "president") {
    // return next();
    alert("请登录之后再进行操作");
    return next({ path: "/login" });
  }
});

new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')
